import {
  wxRequest
} from '../../../utils/server.js';
const _wxRequest = new wxRequest();
let util = require('../../../utils/util.js');
let moment = require('../../../utils/moment.js');
const app = getApp()
Page({
  data: {
    imgUrl: app.globalData.imgUrl,
    cententDetail: {},
    uploadUrl: wx.getStorageSync('uploadUrl'),
    infoList: [{
      name: '公开发布',
      infoindex: 0,
    },
    {
      name: '平台发布',
      infoindex: 1,
    }
    ],
    infoindex: 1,
    editId: 0
  },


  onLoad: function (options) {
    let editId = options.id
    if (editId) {
      this.setData({ editId })
      this.getDataById()
    }
  },

  onShow: function () {
    this.setData({
      orgType: wx.getStorageSync('userInfo').orgType
    })
  },

  onUnload: function () {
  },

  onHide: function () {
  },

  getDataById() {
    var url = '/com/protected/comm/beanCrud/com.CategoryVO/getById/' + this.data.editId;
    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'GET',
      data: {}
    }, (res) => {
      const vo =  res.vo
      let infoindex = 1
      this.data.infoList.map(e=>{
        if(e.infoindex == vo.auth){
          infoindex = vo.auth
        }
      })
      this.setData({ cententDetail:vo,infoindex:infoindex })
    })

  },
  //表单提交
  formSubmit: function (e) {
    console.log(e)
    var formdata = {}
    formdata.catName = e.detail.value.catName;
    if (!formdata.catName) {
      wx.showToast({
        icon: 'none',
        title: '名称不能为空',
      })
      return
    }

    formdata.auth = this.data.cententDetail.auth
    this.formRequest(formdata);
  },

  authTypeChange(e) {
    let auth = e.detail.value;
    let cententDetail = this.data.cententDetail
    cententDetail.auth = auth
    this.setData({ cententDetail,infoindex:auth })
  },



  //请求
  formRequest: function (formdata) {
    var url = ''
    let params = {}

    if (this.data.editId) {
      formdata.id = this.data.editId
      params = formdata
      url = '/com/protected/comm/beanCrud/com.CategoryVO/update';
    } else {
      url = '/com/protected/comm/beanCrud/com.CategoryVO/create';
      params = {
        catName: formdata.catName,
        level: 1
      }
    }

    var title = "发布成功"


    _wxRequest.wxTonkenAjax({
      url: app.globalData.apiUrl + url,
      method: 'POST',
      data: params
    }, (res) => {
      console.log(res)
      wx.showToast({
        title,
      })
      setTimeout(() => {
        wx.redirectTo({
          url: '/pages/square/category/list',
        })
      }, 1500)

    })
  },

})